<template>
	<div>
		<div class="h2">{{ $route.meta.name }}管理</div>
		<div id="components-form-demo-advanced-search">
			<a-form class="ant-advanced-search-form home-form" :form="form" @submit="handleSearch">
				<a-row :gutter="24">
					<a-col :span="8">
						<a-form-item label="活动类型">
							<a-input autocomplete="off" class="field-right" placeholder="请输入活动类型搜索"
								v-decorator="[`key`]" />
						</a-form-item>
					</a-col>
				</a-row>
			</a-form>
		</div>
		<a-button-group size="default" class="option">
			<a-button type="primary" @click="handleSearch">搜索</a-button>
			<a-button @click="handleReset">重置</a-button>
			<a-button type="primary" @click="addList">新增</a-button>
		</a-button-group>
		<div>
			<el-table ref="tableList" :data="data" v-loading="loading" border :max-height="tableHeight"
				highlight-current-row style="width: 100%;">
				<el-table-column type="index" label="序号" width="60" fixed="left"></el-table-column>
				<el-table-column prop="ActivityTypeName" label="活动类型" min-width="160"></el-table-column>
				<el-table-column prop="Score" label="分值" min-width="60">
					<template slot-scope="scope">
						<a-tag color="cyan">{{ scope.row.Score }}</a-tag>
					</template>
				</el-table-column>
				<el-table-column prop="Score" label="添加人" min-width="100">
					<template slot-scope="scope">
						<div>{{ scope.row.AddUserName }} / {{ scope.row.AddUserNo }}</div>
					</template>
				</el-table-column>
				<el-table-column prop="AddTime" label="添加时间" min-width="100"></el-table-column>
				<el-table-column label="操作" width="120" fixed="right">
					<template slot-scope="scope">
						<oprateBtn title="修改" icon="icon-editor1" color="#1890FF"
							@tap="modifyList(scope.row, 'modify')"></oprateBtn>
						<oprateBtn title="删除" icon="icon-delete" color="red" @tap="deleteList(scope.row, 1)">
						</oprateBtn>
					</template>
				</el-table-column>
			</el-table>
			<!-- <el-pagination
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				:current-page="pagination.currentPage"
				:page-sizes="pagination.pageSizeOptions"
				:page-size="pagination.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="pagination.total"
			></el-pagination> -->
		</div>
		<add :text="text" :ActivityTypeList="ActivityTypeList" v-show="addIf" @closeFun="closeFun"></add>
	</div>
</template>

<script>
	import add from './add';
	import Func from '@/utils/func';
	import moment from 'moment';
	import Cache from '@/utils/cache';
	import {
		ManageExtraScoreItem,
		GetActivityTypeList
	} from '@/api/follow';
	const data = [];
	export default {
		components: {
			add
		},
		data() {
			return {
				data,
				form: this.$form.createForm(this, {
					name: 'advanced_search'
				}),
				updateForm: this.$form.createForm(this, {
					name: 'form12345'
				}),
				pagination: {
					currentPage: 1,
					pageSize: this.global.pageSize,
					pageSizeOptions: this.global.pageSizeOptions, // 每页中显示的数据
					showTotal: total => `共有 ${total} 条数据` // 分页中显示总的数据
				},
				addIf: false,
				text: {},
				visible: false,
				confirmLoading: false,
				itemId: '',
				gradeYear: null,
				yearPickShow: false,
				departmentList: [],
				worklist: [],
				loading: false,
				values: {},
				tableHeight: parseFloat(window.innerHeight - 160),
				ActivityTypeList: [],
				requestUrl: this.global.requestUrl,
				token: Cache.get('token'),
				isAssistantList: this.global.isAssistantList
			};
		},
		computed: {},
		created() {
			this.getList();
			this.getOtherList();
		},
		mounted() {},
		methods: {
			moment,
			handleSearch(e) {
				e.preventDefault();
				this.form.validateFields((error, values) => {
					this.pagination.currentPage = 1;
					this.values = values;
					this.getList();
				});
			},
			handleReset() {
				this.form.resetFields();
				this.values = {};
				this.getList();
			},
			handleSizeChange(val) {
				this.pagination.pageSize = val;
				this.getList();
			},
			handleCurrentChange(val) {
				this.pagination.currentPage = val;
				this.getList();
			},
			async getList() {
				this.loading = true;
				let data = {};
				data = {
					...this.values,
					ExtraScoreID: this.$route.query.ID,
					pageIndex: this.pagination.currentPage,
					pageSize: this.pagination.pageSize
				};
				let res = await ManageExtraScoreItem('DoGetList', data);
				this.loading = false;
				this.data = res.data.data;
				// const pagination = { ...this.pagination };
				// pagination.total = res.data.pageInfo.TotalItemCount;
				// this.pagination = pagination;
				this.$refs.tableList.bodyWrapper.scrollTop = 0;
			},
			async getOtherList() {
				let res = await GetActivityTypeList();
				this.ActivityTypeList = this.toTree(res.data.data);
			},
			toTree(data) {
				let result = [];
				let map = {};
				if (!Array.isArray(data)) {
					return [];
				}
				data.forEach(item => {
					map[item.ID] = item;
				});
				data.forEach(item => {
					let parent = map[item.ParentID];
					if (parent) {
						(parent.children || (parent.children = [])).push(item);
					} else {
						result.push(item);
					}
				});
				return result;
			},
			addList() {
				this.text = {};
				this.addIf = !this.addIf;
			},
			modifyList(text) {
				text = Object.assign(text, {
					time: new Date()
				});
				this.addIf = !this.addIf;
				this.text = text;
			},
			deleteList(text, type) {
				let that = this;
				let content = '';
				switch (type) {
					case 1:
						content = '您确定删除该数据吗?';
						break;
					default:
						break;
				}
				this.$confirm({
					title: '提示',
					content: content,
					okText: '确认',
					cancelText: '取消',
					async onOk() {
						let data = {};
						data.id = text.ID;
						let res = await ManageExtraScoreItem('DoDelete', data)
						if (res.data.code == 0) {
							that.getList();
							that.$message.success(res.data.msg);
						} else {
							that.$message.error(res.data.msg);
						}
					}
				});
			},
			seeList(text, name) {
				this.$router.push({
					name: 'StudentManageDetail',
					query: {
						ID: text.ID
					}
				});
			},
			closeFun(data) {
				this.addIf = false;
				if (data == 1) {
					this.getList();
				}
			}
		}
	};
</script>

<style lang="less" scoped>
	.home-form /deep/.ant-form-item {
		display: flex;
		margin-bottom: 10px !important;
	}

	.home-form /deep/.ant-form-item-label {
		width: 80px;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
		text-align: right;
	}

	/deep/.ant-form-item-control-wrapper {
		flex: 1;
	}

	/deep/.ant-calendar-picker {
		width: 100%;
	}
</style>
